<template>
  <div class="blog-detail-container">
    <el-card shadow="hover" v-loading="loading">
      <template #header>
        <div class="card-header">
          <el-button :icon="ArrowLeft" @click="handleBack">返回</el-button>
        </div>
      </template>
      
      <div v-if="blogData" class="blog-content">
        <!-- 标题 -->
        <h1 class="blog-title">{{ blogData.title }}</h1>
        
        <!-- 元信息 -->
        <div class="blog-meta">
          <el-tag
            v-for="category in blogData.categories"
            :key="category.id"
            type="info"
            class="meta-tag"
          >
            {{ category.name }}
          </el-tag>
          <el-tag :type="blogData.status === 1 ? 'success' : 'info'" class="meta-tag">
            {{ blogData.status === 1 ? '已发布' : '草稿' }}
          </el-tag>
          <span v-if="blogData.created_at" class="meta-time">
            创建时间：{{ formatDateTime(blogData.created_at) }}
          </span>
          <span v-if="blogData.updated_at" class="meta-time">
            更新时间：{{ formatDateTime(blogData.updated_at) }}
          </span>
        </div>
        
        <!-- 封面图片 -->
        <div v-if="blogData.cover" class="blog-cover">
          <el-image
            :src="blogData.cover"
            fit="cover"
            style="width: 100%; max-height: 400px; border-radius: 8px;"
            :preview-src-list="[blogData.cover]"
          />
        </div>
        
        <!-- 摘要 -->
        <div v-if="blogData.summary" class="blog-summary">
          <p>{{ blogData.summary }}</p>
        </div>
        
        <!-- 标签 -->
        <div v-if="blogData.tags && blogData.tags.length > 0" class="blog-tags">
          <el-tag
            v-for="tag in blogData.tags"
            :key="tag.id"
            :style="{ color: tag.color || undefined, borderColor: tag.color || undefined }"
            type="primary"
            effect="plain"
            class="tag-item"
          >
            {{ tag.name }}
          </el-tag>
        </div>
        
        <!-- 内容 -->
        <div class="blog-body">
          <div class="content-html" v-html="blogData.content"></div>
        </div>
      </div>
      
      <el-empty v-else description="博客不存在或已删除" />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ArrowLeft } from '@element-plus/icons-vue'
import { getBlogDetail } from '@/api/blogs'
import { formatDateTime } from '@/utils'

interface Category {
  id: string
  name: string
  description?: string
  sort_order?: number
  created_at?: string
  updated_at?: string
}

interface Tag {
  id: string
  name: string
  color?: string
  description?: string
  created_at?: string
  updated_at?: string
}

interface Blog {
  id: string
  title: string
  content: string
  summary?: string
  cover?: string
  status?: number
  categories?: Category[]
  tags?: Tag[]
  created_at?: string
  updated_at?: string
}

const route = useRoute()
const router = useRouter()
const loading = ref(false)
const blogData = ref<Blog | null>(null)


// 获取博客详情
const fetchBlogDetail = async (): Promise<void> => {
  const id = route.params.id as string
  if (!id) {
    ElMessage.error('博客ID不存在')
    router.push({ name: 'Blogs' })
    return
  }
  
  loading.value = true
  try {
    const res = await getBlogDetail({ id })
    if (res.code === 200 && res.data) {
      blogData.value = res.data
    } else {
      ElMessage.error('获取博客详情失败')
      router.push({ name: 'Blogs' })
    }
  } catch (error) {
    console.error('获取博客详情失败:', error)
    ElMessage.error('获取博客详情失败')
    router.push({ name: 'Blogs' })
  } finally {
    loading.value = false
  }
}

// 返回
const handleBack = (): void => {
  router.push({ name: 'Blogs' })
}

onMounted((): void => {
  fetchBlogDetail()
})
</script>

<style lang="scss" scoped>
.blog-detail-container {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  
  .blog-content {
    .blog-title {
      font-size: 28px;
      font-weight: 600;
      color: #333;
      margin: 0 0 20px 0;
      line-height: 1.4;
    }
    
    .blog-meta {
      display: flex;
      align-items: center;
      gap: 12px;
      margin-bottom: 20px;
      padding-bottom: 20px;
      border-bottom: 1px solid #e8e8e8;
      
      .meta-tag {
        margin-right: 8px;
      }
      
      .meta-time {
        font-size: 14px;
        color: #999;
        margin-left: auto;
      }
    }
    
    .blog-cover {
      margin-bottom: 30px;
    }
    
    .blog-summary {
      background-color: #f5f7fa;
      padding: 20px;
      border-radius: 8px;
      margin-bottom: 30px;
      border-left: 4px solid #409eff;
      
      p {
        margin: 0;
        font-size: 16px;
        color: #666;
        line-height: 1.8;
      }
    }
    
    .blog-tags {
      display: flex;
      flex-wrap: wrap;
      gap: 8px;
      margin-bottom: 30px;
      
      .tag-item {
        margin: 0;
      }
    }
    
    .blog-body {
      margin-top: 30px;
      
      .content-html {
        font-size: 16px;
        line-height: 1.8;
        color: #333;
        
        :deep(p) {
          margin: 0 0 16px 0;
        }
        
        :deep(h1),
        :deep(h2),
        :deep(h3),
        :deep(h4),
        :deep(h5),
        :deep(h6) {
          margin: 24px 0 16px 0;
          font-weight: 600;
          color: #333;
        }
        
        :deep(h1) {
          font-size: 28px;
        }
        
        :deep(h2) {
          font-size: 24px;
        }
        
        :deep(h3) {
          font-size: 20px;
        }
        
        :deep(ul),
        :deep(ol) {
          margin: 0 0 16px 0;
          padding-left: 30px;
        }
        
        :deep(li) {
          margin: 8px 0;
        }
        
        :deep(blockquote) {
          margin: 16px 0;
          padding: 12px 20px;
          border-left: 4px solid #409eff;
          background-color: #f5f7fa;
          color: #666;
        }
        
        :deep(code) {
          background-color: #f5f7fa;
          padding: 2px 6px;
          border-radius: 4px;
          font-family: 'Courier New', monospace;
          font-size: 14px;
        }
        
        :deep(pre) {
          background-color: #f5f7fa;
          padding: 16px;
          border-radius: 8px;
          overflow-x: auto;
          margin: 16px 0;
          
          code {
            background-color: transparent;
            padding: 0;
          }
        }
        
        :deep(img) {
          max-width: 100%;
          height: auto;
          border-radius: 8px;
          margin: 16px 0;
        }
        
        :deep(a) {
          color: #409eff;
          text-decoration: none;
          
          &:hover {
            text-decoration: underline;
          }
        }
        
        :deep(table) {
          width: 100%;
          border-collapse: collapse;
          margin: 16px 0;
          
          th,
          td {
            border: 1px solid #e8e8e8;
            padding: 8px 12px;
            text-align: left;
          }
          
          th {
            background-color: #f5f7fa;
            font-weight: 600;
          }
        }
      }
    }
  }
}
</style>

